<template>
  <div class="y-conter">
    <div class="y-biaoge">
      <h3 class="y-bold">报警记录</h3>
      <!-- 搜索输入框部分 -->
      <div>
        <el-form :inline="true" ref="ruleForm" :model="ruleForm" class="demo-form-inline">
          <el-form-item label="设备类型:" prop="equipment_type_id">
            <!-- 设置disabled禁用 -->
            <el-select v-model="ruleForm.equipment_type_id" placeholder="全部">
              <el-option
                v-for="item in input1"
                :key="item.index"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <!-- 按钮搜和索部分 -->
          <el-form-item>
            <el-button type="primary" @click="search('ruleForm')">搜索</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格部分 -->
      <div class="y-table">
        <el-table
          :border="true"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          ref="multipleTable"
          :data="currList"
          tooltip-effect="dark"
          style="width: 98%, text-align: center "
        >
          <el-table-column align="center" type="index" width="50px" :index="indexFn" label="序号"></el-table-column>
          <el-table-column label="设备类型" prop="equipment_type_id" hight="30px" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.equipment_type_id == '1'">门禁设备</span>
              <span v-if="scope.row.equipment_type_id == '2'">塔吊设备</span>
              <span v-if="scope.row.equipment_type_id == '3'">监控设备</span>
              <span v-if="scope.row.equipment_type_id == '4'">电表设备</span>
              <span v-if="scope.row.equipment_type_id == '5'">环境监测设备</span>
              <span v-if="scope.row.equipment_type_id == '6'">车辆监测设备</span>
              <span v-if="scope.row.equipment_type_id == '7'">基坑监测设备</span>
            </template>
          </el-table-column>
          <el-table-column label="报警时间" prop="alarminfo_start" align="center"></el-table-column>
          <el-table-column label="处理时间" prop="alarminfo_deal" align="center"></el-table-column>
          <el-table-column label="处理说明" prop="alarminfo_text" align="center"></el-table-column>
          <el-table-column label="状态" prop="alarminfo_status" align="center">
            <template slot-scope="scope">
              <!-- <span v-text="scope.row.alarminfo_status == '0' ? '未处理' : '已处理'"></span> -->
              <span v-if="scope.row.alarminfo_status==0" style="color:red">未处理</span>
               <span v-if="scope.row.alarminfo_status==1" style="color:green">已处理</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <template slot-scope="scope">
              <el-button type="text" @click="bianji(scope.$index, scope.row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 编辑对话框 -->
        <el-dialog title="编辑信息" :visible.sync="dialogVisible" width="680px">
          <el-form status-icon :rules="rules" ref="twoform">
            <!-- <el-row>
              <el-col :span="6">
                <el-form-item label="设备类型:">
                  <el-input
                    v-model="formInline.equipment_type_id"
                    :readonly="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="设备名称:">
                  <el-input
                    v-model="formInline.equipment_name"
                    :readonly="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="报警时间:">
                  <el-input
                    v-model="formInline.alarminfo_start"
                    :readonly="true"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>-->
            <el-row>
              <el-col :span="6">
                <el-form-item label="处理时间:" prop="alarminfo_deal">
                  <el-date-picker
                    v-model="twoform.alarminfo_deal"
                    type="datetime"
                    placeholder="选择日期时间"
                     format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
                  ></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="处理说明:" prop="alarminfo_text">
                  <el-input v-model="twoform.alarminfo_text" 
  class="padding"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="状态" prop="alarminfo_status">
                  <el-select v-model="twoform.alarminfo_status" placeholder="请选择">
                    <el-option label="未处理" :value="0"></el-option>
                    <el-option label="已处理" :value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="quxiao('twoform')">取 消</el-button>
            <el-button type="primary" @click="SureEdit('twoform')">确 定</el-button>
          </span>
        </el-dialog>
      </div>
      <!-- 分页器 -->
      <div class="y-fenye">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="tableData.length"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import http from "../api";
export default {
  data() {
    return {
      //搜索输入框
      ruleForm: {
        equipment_type_id: "", //设备类型
      },
      // 编辑弹框输入框
      formInline: {
        equipment_type_id: "",
        equipment_type_name: "",
        equipment_name: "",
        alarminfo_start: "",
        alarminfo_text: "",
        alarminfo_status: "",
        alarminfo_deal: "",
      },
      twoform: {
        alarminfo_id: "",
        alarminfo_status: "",
        alarminfo_deal: "",
        alarminfo_text: "",
      },

      dialogVisible: false,
      rules: {}, //验证

      input1: [
        {
          value: 0,
          label: "全部",
        },
        {
          value: 1,
          label: "门禁设备",
        },
        {
          value: 2,
          label: "塔吊设备",
        },
        {
          value: 3,
          label: "监控设备",
        },
        {
          value: 4,
          label: "电表设备",
        },
        {
          value: 5,
          label: "环境监测设备",
        },
        {
          value: 6,
          label: "车辆监测设备",
        },
        {
          value: 7,
          label: "基坑监测设备",
        },
      ],
      // 分页
      tableData: [], //总数居
      currPage: 1, //当前页
      pageSize: 7, //每页数据量
      currList: [], //当前展示的数据数组
      pickerOptions1: {
        //  日期
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      value1: "",
      value2: "",
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      value1: "",
      value2: "",
      value3: "",
    };
  },
  computed: {},

  methods: {
    // 分页
    setCurrList(index) {
      this.currList = this.tableData.slice(
        (index - 1) * this.pageSize,
        index * this.pageSize
      );
      // console.log('当前页',index);
    },
    //改变当前页
    changePage(val) {
      this.currPage = val;
      this.setCurrList(val);

      // console.log(val);
    },
    // 表格序号
    indexFn(index) {
      index = index + 1 + (this.currPage - 1) * this.pageSize;
      return index;
    },
    //重置按钮
    resetForm(formName) {
      this[formName] = {};
      this.$refs[formName].resetFields();
    },

    // 编辑
    bianji(index, row) {
      console.log("row", row);

      this.dialogVisible = true;
      this.twoform.alarminfo_id = row.alarminfo_id; //类型

      this.twoform.alarminfo_text = row.alarminfo_text; //说明
      this.twoform.alarminfo_status = row.alarminfo_status; //状态
      this.twoform.alarminfo_deal = row.alarminfo_deal; //处理时间

      // console.log("的说法不接受大V几号放假库存",this.twoform);
    },
    // 编辑里的确定按钮
    SureEdit(formName) {
      console.log("的说法不接受大V几号放假库存",this.twoform);
      this.dialogVisible = false;
       http.modification(this.twoform).then((r) => {
           
            http.demand({}).then((res) => {
              this.tableData = res.data;
              this.setCurrList(1);
            });
          });
      // this.$refs[formName].validate((valid) => {
      //   console.log("编辑后的",this.twoform)
      //   if (valid) {
      //     http.modification(this.twoform).then((r) => {
      //       console.log("这是编辑的", r.data);
      //       http.demand({}).then((res) => {
      //         this.tableData = res.data;
      //         this.setCurrList(1);
      //       });
      //     });
      //   } else {
      //     this.$message({
      //       showClose: true,
      //       message: "修改失败",
      //       type: "error",
      //     });
      //     return false;
      //   }
      // });
      // this.$refs[formName].validate((valid) => {
      //   if (valid) {
      //     http.modification(this.formInline).then((r) => {
      //       console.log("这是编辑的", r.data);
      //       http.demand({}).then((res) => {
      //         this.tableData = res.data;
      //         this.setCurrList(1);
      //       });
      //     });
      // this.formInline
      // this.dialogVisible = false;
      // this.$refs[formName].validate((valid) => {
      //   if (valid) {
      //   } else {
      //     this.$message({
      //       showClose: true,
      //       message: "修改失败",
      //       type: "error",
      //     });
      //     return false;
      //   }
      // });
    },
    // 编辑里的取消按钮
    quxiao(formInline) {
      // console.log(formInline);
      this.dialogVisible = false;
      // 点取消后清空
      for (let key in formInline) {
        formInline[key] = "";
      }
    },
    // 表格删除按钮
    handleDelete(index, row) {
      http.delline(row).then((r) => {
        http.demand({}).then((r) => {
          console.log(r);
          this.tableData = r.data;
          console.log(r.data);
          this.setCurrList(1);
        });

        console.log(r);
        console.log("单个删除", r.data);
      });
    },

    // //搜索输入
    // search(ruleForm) {
    //   this.$refs[ruleForm].validate((valid) => {
    //     if (valid) {
    //       http.demand(this.ruleForm).then((r) => {
    //         console.log("返回的数据", r);
    //         this.tableData = r.data;
    //         this.setCurrList(1);
    //         console.log(this.ruleForm);
    //       });
    //     } else {
    //       console.log("error submit!!");
    //       return false;
    //     }
    //   });
    // },
    // //搜索输入
    search(ruleForm) {
      console.log("这是搜索", this.ruleForm);
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          http.demand(this.ruleForm).then((r) => {
            console.log("返回的数据", r);
            // http.demand({}).then((r) => {
            //   this.tableData = r.data;
            //   this.setCurrList(1);
            // });
            this.tableData = r.data;
            console.log("新的数据", this.tableData);
            this.setCurrList(1);
            // console.log(this.ruleForm);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  //获取数据
  mounted() {
    http.sougetBuild({}).then((r) => {
      console.log(r.data, "r.data");
      //  this.input1 = r.data;
      // let obj={}
      // obj.equipment_type_id='',
      // obj.equipment_type_name='全部'
      // this.input1.unshift(obj);
    });
    // 查询数据
    http.demand({}).then((r) => {
      console.log("数据", r.data);
      this.tableData = r.data;
      this.setCurrList(1);
    });
  },
};
</script>
<style scoped>
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px;
  /* margin-left: 5px; */
}
.padding >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px !important;
}
/* 大容器 */
.y-conter {
  height: 640px;
  background-color: white;
  overflow: hidden;
  overflow-y: scroll;
  margin-top: 15px;
}
/* 表格 */
.y-biaoge {
  margin-top: 20px;
  margin-left: 10px;
}

.y-table {
  width: 100%;
  margin-top: 15px;
}
/* 右侧新增导出按钮 */
.y-btn {
  position: absolute;
  top: 164px;
  left: 94%;
}
/* 所有按钮 */
.el-button {
  height: 30px;
  line-height: 0px;
}
/* 标题字体 */
.y-bold {
  font-weight: bold;
}
/* 输入框部分样式 */
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
}
.el-input >>> .el-input__inner {
  width: 178px;
  height: 30px;
}
.el-range-editor {
  width: 160px;
  height: 30px;
  padding-left: 10px;
}
.el-input >>> .el-range__icon {
  line-height: 0px;
  padding-left: 10px;
}
.el-range-editor >>> .el-input__inner {
  width: 200px;
  height: 30px;
  margin-top: 10px;
}
/* 分页 */
.y-fenye {
  float: right;
  margin-top: 20px;
}
/* 新增 */

.el-form-item {
  margin-bottom: 2px !important;
}
.el-col-6 {
  width: 25%;
  margin-left: 39px;
}
</style>